//
//  MYCreditsMallToOrderVC.m
//  MuYing
//
//  Created by tbb on 2020/3/3.
//  Copyright © 2020 TBB. All rights reserved.
//
//完成
#import "MYCreditsMallToOrderVC.h"
#import "MYCreditsMallToOrderModel.h"
#import "YQPAddressModel.h"
#import "YQPIntegerPayView.h"
@interface MYCreditsMallToOrderVC ()
@property (strong, nonatomic) YQPIntegerPayView *integeView;
@end

@implementation MYCreditsMallToOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getGoodsInfo];
}
-(void)getGoodsInfo {
    
    YQSHOWLOADINGTIPS(@"")
    [YQPNetWorkingManager sendPostRequest:kPOST_INTEGER_EXCHANGE_URL(self.goodsID) parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            self.integralGoodsModel = [MYCreditsMallToOrderModel modelWithJSON:responseObject.data];
            YQPAddressEntityModel *adderModel = [YQPAddressEntityModel new];
            adderModel.userAddress = self.integralGoodsModel.receiverDetailAddress;
            adderModel.city = self.integralGoodsModel.receiverCity;
            adderModel.province = self.integralGoodsModel.receiverProvince;
            adderModel.userPhone = self.integralGoodsModel.receiverPhone;
            adderModel.name = self.integralGoodsModel.receiverName;
            adderModel.addressId = self.integralGoodsModel.addressId;
            self.addresModel = adderModel;
            
            YQPCartModel *cartModel = [YQPCartModel new];
            cartModel.skuId = self.integralGoodsModel.integralGoodsId;
            cartModel.itemNumber = @"1";
            cartModel.skuImg = self.integralGoodsModel.defaultImage;
            cartModel.specification = @"";
            cartModel.price = self.integralGoodsModel.consumeIntegral;
            cartModel.skuName = self.integralGoodsModel.goodsName;
            cartModel.freight = self.integralGoodsModel.freight;
            
            self.selectGoodArray = @[cartModel];
            self.bottonView.priceLb.text = [NSString stringWithFormat:@"%@: %@ %@",kYQLS(@"总计"),self.integralGoodsModel.totalAmount,kYQLS(@"积分")];
            [self.listTableView reloadData];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error)
    }];
    
}

//提交订单
-(void)subOrderAction:(UIButton *)sender {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"addressId"] = self.addresModel.addressId;
    parameters[@"integralGoodsId"] = self.integralGoodsModel.integralGoodsId;
    parameters[@"message"] = self.message;
    parameters[@"totalIntegralAmount"] = self.integralGoodsModel.totalAmount;
    YQSHOWLOADINGTIPS(kYQLS(@"正在生成订单"))
    [YQPNetWorkingManager sendPostRequest:kPOST_COMMIT_ORDER_URL parameters:parameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        
        if (responseObject.code == 0) {
            self.integeView.labSubTitle.text = [NSString stringWithFormat:@"%@ %@",self.integralGoodsModel.totalAmount,kYQLS(@"积分")];
            
            [self.integeView show];
            MJWeakSelf
            [self.integeView setSureBlock:^{
                [weakSelf.integeView dismissView];
                [weakSelf payOrderByIntegerApi:responseObject.data];
            }];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error)
    }];
    
}

#pragma mark - 积分支付
- (void)payOrderByIntegerApi:(NSString *)integralOrderId
{
    NSString *url = [NSString stringWithFormat:@"%@%@",kPOST_INTEGER_PAY_URL,integralOrderId];
    YQSHOWLOADINGTIPS(kYQLS(@"正在支付"))
    [YQPNetWorkingManager sendPostRequest:url parameters:@{} success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            [MBProgressHUD showMessage:kYQLS(@"支付成功")];
            MYShopSalesOrderSucceedVC *succeedVC = [[MYShopSalesOrderSucceedVC alloc]initWithNibName:@"MYShopSalesOrderSucceedVC" bundle:nil];
            succeedVC.isIntegral = YES;
            succeedVC.navigationItem.title = kYQLS(@"支付成功");
            succeedVC.paySuceedLb.text = kYQLS(@"订单支付成功");
            succeedVC.paySuceedBtnText = kYQLS(@"查看我的订单");
            [self.navigationController pushViewController:succeedVC animated:true];
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error)
    }];
}


- (YQPIntegerPayView *)integeView
{
    if (!_integeView) {
        _integeView = [YQPIntegerPayView new];
    }
    return _integeView;
}
@end
